package com.hhkj.admin.bank.common.component.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.admin.bank.common.component.checker.BankCreateChecker;
import com.hhkj.admin.bank.common.component.creator.BankCreator;
import com.hhkj.admin.bank.common.utils.BankCreateUtils;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.model.req.BankReq;
import com.hhkj.admin.bank.model.res.BankRes;
import com.hhkj.admin.question.model.req.QuestionReq;

import java.util.List;

/**
 * BankHandler
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/7/11 17:08
 */
public class BankHandler
{

    /**
     * 根据规则处理题库信息
     *
     * @param bankRes                总题库
     * @param banRule                题库规则信息
     * @param totalQuestionGroupList 总题目组列表
     * @param errorList              错误信息列表
     * @param bankReqList            需要生成的题库信息列表
     */
    public static void handlerBankByRule(
            BankRes bankRes,
            BankRule banRule,
            List<QuestionReq> totalQuestionGroupList,
            List<String> errorList,
            List<BankReq> bankReqList)
    {
        // 题库信息创建
        List<QuestionReq> practiceQuestionGroupList
                = BankCreateUtils.filterPracticeQuestionGroupListByRule(banRule, totalQuestionGroupList);
        // 剩余题目信息
        List<QuestionReq> residueQuestionGroupList
                = BankCreateUtils.filterResidueQuestionGroupList(totalQuestionGroupList, practiceQuestionGroupList);

        BankReq practiseBankReq
                = BankCreator.createPractiseBank(bankRes, banRule.getId(), practiceQuestionGroupList);

        // BankReq practiseBankReq = BankCreator.createPractiseBank(bankRes, courseBankRule, bankRes.getQuestionGroupList());
        BankReq testBank
                = BankCreator.createTestBank(bankRes, banRule.getId(), residueQuestionGroupList);

        List<String> testAmountCofErrorList
                = BankCreateChecker.checkerAndConfTestAmountIfNotError(testBank, banRule, practiceQuestionGroupList, residueQuestionGroupList);
        if (CollectionUtil.isNotEmpty(testAmountCofErrorList)) {
            errorList.addAll(testAmountCofErrorList);
        }

        List<String> errorInfoList = banRule.matchBankErrorInfo(testBank);
        if (CollectionUtil.isNotEmpty(errorInfoList)) {
            errorList.addAll(errorInfoList);
        }
        bankReqList.add(practiseBankReq);
        bankReqList.add(testBank);
    }
}
